package cn.gson.crm.controller.student;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.AccessoryDao;
import cn.gson.crm.model.dao.OrderDao;
import cn.gson.crm.model.dao.RecordsDao;
import cn.gson.crm.model.entity.Accessory;
import cn.gson.crm.model.entity.Records;
import cn.gson.crm.vo.OrderVo;
import cn.gson.crm.vo.RecordsVo;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/student")
@MultipartConfig(maxFileSize = 1024*1024 , fileSizeThreshold =1024*1024)
public class StudentServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private OrderDao orderDao = new OrderDao();
	private RecordsDao recordDao = new RecordsDao();
	private AccessoryDao accessoryDao = new AccessoryDao();
	
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
	String page  = request.getParameter("page");
	String search = request.getParameter("search");
	
	if (page == null || page.isEmpty()) {
		page = "1";
	}
	
	//当前页数据
	List<OrderVo> orderList = orderDao.getOrderAllList(12,Integer.parseInt((String)page),search);
	List<OrderVo> orderList2 = orderDao.getOrderAllList(3,Integer.parseInt((String)page),search);
	List<OrderVo> orderList3 = orderDao.getOrderAllList(11,Integer.parseInt((String)page),search);
	//总页数
	int totalPage = orderDao.getTotalPage(12,search);
	int totalPage2 = orderDao.getTotalPage(3,search);
	int totalPage3 = orderDao.getTotalPage(11,search);
	
	request.setAttribute("queryString", noPageQueryString(request));
	request.setAttribute("orderList", orderList);
	request.setAttribute("orderList2", orderList2);
	request.setAttribute("orderList3", orderList3);
	request.setAttribute("totalPage", totalPage);
	request.setAttribute("totalPage2", totalPage2);
	request.setAttribute("totalPage3", totalPage3);
	request.setAttribute("page", page);

	//查看签约的文件信息
	String upid = request.getParameter("upid");
	if(upid != null){
		System.out.println("upidupid"+upid);
		List<Accessory> accessory = accessoryDao.getAllAccessoryByModel(Integer.parseInt(upid),"order");
		request.setAttribute("accessory", accessory.get(0).getFilePath());
	}
	
	forward("student/stu_manager.jsp");
	return;
}

/**
 * 
 * @param request
 * @return
 */
private String noPageQueryString(HttpServletRequest request) {
	Map<String, String[]> params = request.getParameterMap();

	String paramsStr = "";
	int i = 0;
	for (String key : params.keySet()) {
		if ("page".equals(key)) {
			continue;
		}
		String[] valus = params.get(key);
		String query = "";
		for (int j = 0; j < valus.length; j++) {
			query += key + "=" + valus[j];
			if (j < valus.length - 1) {
				query += "&";
			}
		}
		paramsStr += query;
		if (i < params.keySet().size() - 1) {
			paramsStr += "&";
		}
		i++;
	}
	return paramsStr.isEmpty() ? "" : "&" + paramsStr;
}
}
